package com.longshen.dubbo.api.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.longshen.dubbo.api.mapper.AdminRoleMapper;
import com.longshen.dubbo.api.mapper.AdminRoleMenuMapper;
import com.longshen.dubbo.api.mapper.AdminTradeMapper;
import com.longshen.dubbo.api.model.AdminRole;
import com.longshen.dubbo.api.model.AdminRoleMenu;
import com.longshen.dubbo.api.model.AdminTrade;
import com.longshen.dubbo.api.sequence.SequenceService;
import com.longshen.dubbo.api.service.AdminRoleService;

import tk.mybatis.mapper.entity.Example;

@Service
public class AdminRoleServiceImpl implements AdminRoleService {
	@Autowired
	private AdminRoleMapper mapper;
	@Autowired
	private AdminRoleMenuMapper roleMenuMapper;
	
	@Autowired
	private AdminTradeMapper tradeMapper;
	
	


	@Autowired
	private SequenceService seq;

	@Override
	public AdminRole add(AdminRole in, List<String> menus) {
		in.setId(seq.getId());
		mapper.insert(in);

		// 添加角色关联菜单
		if (menus != null) {
			menus.forEach(menuId -> {
				AdminRoleMenu roleMenu = new AdminRoleMenu();
				roleMenu.setId(seq.getId());
				roleMenu.setMenuId(menuId);
				roleMenu.setRoleId(in.getId());
				roleMenuMapper.insert(roleMenu);
			});
		}

		return in;
	}

	@Override
	public void delById(String  id) {
		// 删除角色权限
		AdminRoleMenu roleMenu = new AdminRoleMenu();
		roleMenu.setRoleId(id);
		roleMenuMapper.delete(roleMenu);
		// 删除角色
		mapper.deleteByPrimaryKey(id);

	}

	@Override
	public Page<AdminRole> queryPage(Integer pagesize, Integer pagenum, String rolename) {
		Example example = new Example(AdminRole.class);
		if (StringUtils.isNotBlank(rolename)) {
			rolename = "%" + rolename + "%";
			example.createCriteria().andLike("roleName", rolename);
		}
		example.setOrderByClause("id");
		Page<AdminRole> page = PageHelper.startPage(pagenum, pagesize)
				.doSelectPage(() -> mapper.selectByExample(example));
		return page;
	}

	@Override
	public void mod(AdminRole in, List<String> menus) {
		mapper.updateByPrimaryKeySelective(in);
		AdminRoleMenu roleMenu = new AdminRoleMenu();
		roleMenu.setRoleId(in.getId());
		// 删除原有关联
		roleMenuMapper.delete(roleMenu);

		// 添加角色关联菜单
		if (menus != null) {
			menus.forEach(menuId -> {
				AdminRoleMenu add = new AdminRoleMenu();
				add.setId(seq.getId());
				add.setMenuId(menuId);
				add.setRoleId(in.getId());
				roleMenuMapper.insert(add);
			});
		}
	}

	@Override
	public List<AdminRoleMenu> roleMenus(String id) {
		AdminRoleMenu roleMenu = new AdminRoleMenu();
		roleMenu.setRoleId(id);
		return roleMenuMapper.select(roleMenu);
	}

	@Override
	public AdminRole queryById(String id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<AdminRole> all() {
		return mapper.selectAll();
	}

	@Override
	public List<AdminTrade> ownTreade(String roleId) {
		return tradeMapper.ownTrade(roleId);
	}

	@Override
	public List<AdminTrade> UnOwnTreade(String roleId) {
		return tradeMapper.unOwnTrade(roleId);
	}

	
	
	

	
	

}
